package com.xmx.productcenter.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.Query;

import com.xmx.productcenter.dao.GoodsProductDao;
import com.xmx.productcenter.entity.GoodsProductEntity;
import com.xmx.productcenter.service.GoodsProductService;

import javax.annotation.Resource;

import static  com.xmx.common.rabbitDto.OrderDto.*;

@Service("goodsProductService")
@Slf4j
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductDao, GoodsProductEntity> implements GoodsProductService {

    @Resource
    RabbitTemplate rabbitTemplate;

    @Resource
    GoodsProductDao goodsProductDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsProductEntity> page = this.page(
                new Query<GoodsProductEntity>().getPage(params),
                new QueryWrapper<GoodsProductEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public int addStock( Integer orderId,Integer id, Integer num) {
        return goodsProductDao.addStock(id, num);
    }

    @Override
    public int reduceStock( Integer orderId,Integer id, Integer num) {
        log.info("=================开始减少库存=======");
        int i = goodsProductDao.reduceStock(id, num);
        if (i>=0){
            Map map = new HashMap();
            map.put("orderId",orderId);
            map.put("prouductId",id);
            map.put("prouductNum",num);
            rabbitTemplate.convertAndSend(STOCK_EVENT_EXCHANG,STOCK_LOCK_ROUTING,map);
        }
        return i;
    }

}